//
//  DHReturnViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/18.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHReturnViewController.h"
#import "DHOrderDetailAddressCell.h"
#import "DHReturnProductCell.h"
#import "DHReturnCluesCell.h"
#import "Address.h"
#import "DHContactViewController.h"

@interface DHReturnViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *productArr;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (strong, nonatomic) Address *address;

@end

@implementation DHReturnViewController

- (instancetype)initWithProductArray:(NSArray *)productArr
{
    self = [super init];
    if (self) {
        self.productArr = productArr;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"归还";
    
    self.bottomView.layer.cornerRadius = 2;
    self.bottomView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.bottomView.layer.shadowRadius = 5;
    self.bottomView.layer.shadowOpacity = 0.5;
    self.bottomView.layer.shadowOffset  = CGSizeMake(0, 0);
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHOrderDetailAddressCell" bundle:nil] forCellReuseIdentifier:kDHOrderDetailAddressCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHReturnProductCell" bundle:nil] forCellReuseIdentifier:kDHReturnProductCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHReturnCluesCell" bundle:nil] forCellReuseIdentifier:kDHReturnCluesCell];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gaodeaddressselected:) name:@"gaodeaddressselected" object:nil];
    
    [self loadAddressRecent];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"gaodeaddressselected" object:nil];
}

- (void)gaodeaddressselected:(NSNotification *)not{
    if (not){
        NSDictionary *dic = not.object;
        GeoCode *geoCode = [dic objectForKey:@"geoCode"];
        self.address.province = geoCode.province;
        self.address.city = geoCode.city;
        self.address.district = geoCode.district;
        self.address.address = [dic objectForKey:@"detailAddress"];
        [self.tableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }
}

// 获取地址
- (void)loadAddressRecent{
    [[DHNetAPIClient shareDHNetAPIClient] request_address_recentAndBlock:^(id data, NSError *error) {
        if ([data objectForKey:@"address"]) {
            self.address = [Address modelWithDictionary:[data objectForKey:@"address"]];
        }else{
            self.address = [[Address alloc] init];
            self.address.phone = [Login curLoginUser].phone;
            self.address.name = [[Login curLoginUser].realname length] > 0? [Login curLoginUser].realname:[Login curLoginUser].nickname;
            if ([[NSUserDefaults standardUserDefaults] objectForKey:USER_HOME_ADDRESS]) {
                GeoCode *geocode = [GeoCode modelWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_HOME_ADDRESS]];
                self.address.province = geocode.province;
                self.address.city = geocode.city;
                self.address.district = geocode.district;
                self.address.address = geocode.address;
                self.address.logistic = @"普通快递";
                self.address.accurate = @"";
            }
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 确定归还
- (IBAction)returnAction:(id)sender {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[NSString stringWithFormat:@"%@%@%@%@",self.address.province,self.address.city,self.address.district,self.address.address] forKey:@"address"];
    [params setObject:self.address.name forKey:@"contact"];
    [params setObject:self.address.phone forKey:@"mobile"];
    
    NSMutableArray *_idArr = [NSMutableArray array];
    [self.productArr enumerateObjectsUsingBlock:^(NSDictionary *product, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *sizesArr = [product objectForKey:@"sizes"];
        [sizesArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [_idArr addObject:[obj objectForKey:@"_id"]];
        }];
    }];
    [params setObject:_idArr forKey:@"size_ids"];
    
    [[DHNetAPIClient shareDHNetAPIClient] request_products_returnWithParams:params andBlock:^(id data, NSError *error) {
        
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 82;
    }else if (indexPath.row == 1){
        return 100 * [self.productArr count];
    }else{
        return 135;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHOrderDetailAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHOrderDetailAddressCell forIndexPath:indexPath];
        cell.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",self.address.province,self.address.city,self.address.district,self.address.address];
        cell.phoneLabel.text = self.address.phone;
        cell.nameLabel.text = self.address.name;
        return cell;
    }else if (indexPath.row == 1){
        DHReturnProductCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHReturnProductCell forIndexPath:indexPath];
        [cell updateCellWithArr:self.productArr];
        return cell;
    }else{
        DHReturnCluesCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHReturnCluesCell forIndexPath:indexPath];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        DHContactViewController *addressController = [[DHContactViewController alloc] init];
        addressController.address = self.address;
        [self.navigationController pushViewController:addressController animated:YES];
    }
}

@end
